package com.oriental.manage.service.payCount.impl;

import com.oriental.manage.controller.opcif.CountMap;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.dao.payCount.PayTransCountMapper;
import com.oriental.manage.pojo.payCount.*;
import com.oriental.manage.service.payCount.PayTransCountService;
import com.oriental.paycenter.commons.utils.BeanCopier;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * Desc: 查询支付交易统计成功或失败数量报表
 * Date: 2017/5/1
 * User: Chuch
 * See :
 */
@Service
@Log4j
public class PayTransCountServiceImpl implements PayTransCountService {

    @Autowired
    private PayTransCountMapper payTransCountMapper;

    @Override
    public List<PayTransCountRespModel> queryTransCount(PayTransCountModel payTransCountModel) {
        List<PayTransCountRespModel> list = new ArrayList<>();
        PayTransCountRespModel ptcrm;
        Map<String, String> map = new HashMap<>();
        map.put("startTime", payTransCountModel.getStartTime());
        map.put("endTime", payTransCountModel.getEndTime());
        map.put("bankCode", payTransCountModel.getBankCode());
        map.put("transType", "2");
        //成功集合
        List<PayTransCount> sucList = payTransCountMapper.queryTransCount(map);
        map.put("transType", "1");
        //失败集合
        List<PayTransCount> failList = payTransCountMapper.queryTransCount(map);

        /**
         * 值  支付机构+bankCode
         */
        Map<String, PayTransCount> sucMap = new HashMap<>();
        Map<String, PayTransCount> failMap = new HashMap<>();
        /**
         * 取成功与失败集合并集,求返回列表总长度
         */
        Set<String> allSet = new HashSet<>();
        PayTransCount payTransCount;
        for (PayTransCount ptc : sucList) {
            allSet.add(ptc.getOrgCode() + ptc.getBankCode());
            sucMap.put(ptc.getOrgCode() + ptc.getBankCode(), ptc);
        }
        for (PayTransCount ptc : failList) {
            allSet.add(ptc.getOrgCode() + ptc.getBankCode());
            failMap.put(ptc.getOrgCode() + ptc.getBankCode(), ptc);
        }
        for (String s : allSet) {
            ptcrm = new PayTransCountRespModel();
            ptcrm.setStartTime(payTransCountModel.getStartTime());
            ptcrm.setEndTime(payTransCountModel.getEndTime());
            payTransCount = sucMap.get(s);
            if (payTransCount == null) {
                ptcrm.setPaySuccessTotal(0);
                ptcrm.setPaySuccessMoney(0);
            } else {
                ptcrm.setOrgCode(payTransCount.getOrgCode());
                ptcrm.setBankCode(payTransCount.getBankCode());
                ptcrm.setBankName(payTransCount.getBankName());
                ptcrm.setPaySuccessTotal(payTransCount.getTotal());
                ptcrm.setPaySuccessMoney(payTransCount.getTotalAmount());
            }
            payTransCount = failMap.get(s);
            if (payTransCount == null) {
                ptcrm.setPayFailTotal(0);
                ptcrm.setPayFailMoney(0);
            } else {
                ptcrm.setOrgCode(payTransCount.getOrgCode());
                ptcrm.setBankCode(payTransCount.getBankCode());
                ptcrm.setBankName(payTransCount.getBankName());
                ptcrm.setPayFailTotal(payTransCount.getTotal());
                ptcrm.setPayFailMoney(payTransCount.getTotalAmount());
            }
            ptcrm.setPayFailRate(division(ptcrm.getPayFailTotal(), ptcrm.getPayFailTotal() + ptcrm.getPaySuccessTotal()));
            list.add(ptcrm);
        }
        return processList(list);
//        return list;
    }

    /**
     * 处理List
     *
     * @param list
     * @return
     */
    private List<PayTransCountRespModel> processList(List<PayTransCountRespModel> list) {
        List<PayTransCountRespModel> pageList = new ArrayList<>();
        //存放每个机构对应的数据集合
        Map<String, List<PayTransCountRespModel>> orgMap = new HashMap<>();
        List<PayTransCountRespModel> orgList;
        for (PayTransCountRespModel l : list) {
            orgList = orgMap.get(l.getOrgCode());
            if (orgList == null) {
                orgList = new ArrayList<>();
            }
            orgList.add(l);
            orgMap.put(l.getOrgCode(), orgList);
        }
        //单一机构下 存放合并后的银行数据  此时的Map 已经满足运营要求单条的数据
        Map<String, PayTransCountRespModel> tempMergeBankMap;
        PayTransCountRespModel pcrm;
        for (String s : orgMap.keySet()) {
            //存放单机构下银行列表
            orgList = orgMap.get(s);
            tempMergeBankMap = new HashMap<>();
            for (PayTransCountRespModel p : orgList) {
                String bankCode = p.getBankCode().split("_")[0];
                pcrm = tempMergeBankMap.get(bankCode);
                if (pcrm != null) {
                    pcrm.setPaySuccessTotal(pcrm.getPaySuccessTotal() + p.getPaySuccessTotal());
                    pcrm.setPaySuccessMoney(pcrm.getPaySuccessMoney() + p.getPaySuccessMoney());
                    pcrm.setPayFailTotal(pcrm.getPayFailTotal() + p.getPayFailTotal());
                    pcrm.setPayFailMoney(pcrm.getPayFailMoney() + p.getPayFailMoney());
                } else {
                    pcrm = BeanCopier.copy(p, PayTransCountRespModel.class);
                    pcrm.setBankCode(bankCode);
                    pcrm.setBankName(CountMap.getBankName(bankCode) != null ? CountMap.getBankName(bankCode) : bankCode);
                }
                tempMergeBankMap.put(bankCode, pcrm);
            }
            //单机构下银行数据已正常，计算失败比，放入页面list
            for (String bc : tempMergeBankMap.keySet()) {
                pcrm = tempMergeBankMap.get(bc);
                pcrm.setPayFailRate(division(pcrm.getPayFailTotal(), pcrm.getPayFailTotal() + pcrm.getPaySuccessTotal()));
                pageList.add(pcrm);
            }
        }
        return pageList;
    }


    private static String division(int a, int b) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format((float) (a * 100) / b)+"%";
    }


    @Override
    public List<PayTransCountFailRespModel> queryFailReasonRate( PayTransCountFailModel requestModel) {
        PayTransCountFailRespModel ptcfrm;
        List<PayTransCountFailRespModel> list = new ArrayList<>();
        //失败原因条数
        List<PayTransCount> applyCount = payTransCountMapper.queryFailReason(requestModel);
        //查询失败原因总数
        int a = payTransCountMapper.queryFailTotal(requestModel);
        for (PayTransCount payTransCount : applyCount) {
            ptcfrm = new PayTransCountFailRespModel();
            ptcfrm.setStartTime(requestModel.getStartTime());
            ptcfrm.setEndTime(requestModel.getEndTime());
            ptcfrm.setFailReason(payTransCount.getFailReason());
            ptcfrm.setTotal(payTransCount.getTotal());
            ptcfrm.setFailReasonRate(division(payTransCount.getTotal(), a));
            list.add(ptcfrm);
        }
        return list;

    }

    public List<PayTransCountFailDetailRespModel> queryFailDetailRate(PayTransCountFailModel requestModel) {
        List<PayTransCountFailDetailRespModel> list = new ArrayList<>();
        PayTransCountFailDetailRespModel ptcfdrm;
        //查询失败明细条数
        List<PayTransCount> applyCount = payTransCountMapper.queryFailDetail(requestModel);
        //查询某个失败原因的总数
        int b = payTransCountMapper.queryFailReasonTotal(requestModel);
        for (PayTransCount payTransCount : applyCount) {
            ptcfdrm = new PayTransCountFailDetailRespModel();
            ptcfdrm.setOrgCode(payTransCount.getOrgCode());
            ptcfdrm.setBankCode(payTransCount.getBankCode());
            ptcfdrm.setTotal(payTransCount.getTotal());
            ptcfdrm.setFailDetailRate(division(payTransCount.getTotal(), b));
            list.add(ptcfdrm);
        }
        return processList(list, b);

    }


    /**
     * 处理明细List
     *
     * @param list
     * @return
     */
    private List<PayTransCountFailDetailRespModel> processList(List<PayTransCountFailDetailRespModel> list, int b) {
        List<PayTransCountFailDetailRespModel> pageList = new ArrayList<>();
        //存放每个机构对应的数据集合
        Map<String, List<PayTransCountFailDetailRespModel>> orgMap = new HashMap<>();
        List<PayTransCountFailDetailRespModel> orgList;
        for (PayTransCountFailDetailRespModel l : list) {
            orgList = orgMap.get(l.getOrgCode());
            if (orgList == null) {
                orgList = new ArrayList<>();
            }
            orgList.add(l);
            orgMap.put(l.getOrgCode(), orgList);
        }
        //单一机构下 存放合并后的银行数据  此时的Map 已经满足运营要求单条的数据
        Map<String, PayTransCountFailDetailRespModel> tempMergeBankMap;
        PayTransCountFailDetailRespModel pcrm;
        for (String s : orgMap.keySet()) {
            //存放单机构下银行列表
            orgList = orgMap.get(s);
            tempMergeBankMap = new HashMap<>();
            for (PayTransCountFailDetailRespModel p : orgList) {
                String bankCode = p.getBankCode().split("_")[0];
                pcrm = tempMergeBankMap.get(bankCode);
                if (pcrm != null) {
                    pcrm.setTotal(pcrm.getTotal() + p.getTotal());
                } else {
                    pcrm = BeanCopier.copy(p, PayTransCountFailDetailRespModel.class);
                    pcrm.setBankCode(bankCode);
                    pcrm.setBankName(CountMap.getBankName(bankCode) != null ? CountMap.getBankName(bankCode) : bankCode);
                }
                tempMergeBankMap.put(bankCode, pcrm);
            }
            //单机构下银行数据已正常，计算失败比，放入页面list
            for (String bc : tempMergeBankMap.keySet()) {
                pcrm = tempMergeBankMap.get(bc);
                pcrm.setFailDetailRate(division(pcrm.getTotal(), b));
                pageList.add(pcrm);
            }
        }
        return pageList;
    }

}
